package com.nf.service;
import com.nf.entity.House;
import com.nf.entity.HouseInfo;
import com.nf.entity.Student;
import com.nf.entity.StudentHouseInfo;
import com.nf.mappers.StudentMapper;
import com.sun.xml.internal.ws.api.ha.HaInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class StudentserviceImpl implements  Studentservice {

  private  StudentMapper mapper=null;

    public Student test() {
        SqlSessionFactoryBuilder builder=null;
        SqlSession session=null;
        try {
            InputStream inputStream= Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
            session =factory.openSession();
            mapper=session.getMapper(StudentMapper.class);
           System.out.println("实现类是："+mapper);
        } catch (IOException e) {
            e.printStackTrace();
        }
       Student s =mapper.findStudentById(4);
      /*   ----成功！
      System.out.println(s.getName());
      Map<String,Object> student=mapper.findStudentById(2);--用于测试toomanyresult异常
        System.out.print(student.get("studId"));
      List<Student> students=mapper.findallStudents();
        for (Student s1:students) {
            System.out.print(s1.getName()+s1.getEmail());
        }*/

    List<HouseInfo> infos=mapper.getAllhouse();
        for (HouseInfo hi:infos ) {
            System.out.print(hi.getHid()+" "+hi.getName()+" "+hi.getTcount());
            List<Student> students=hi.getStudentList();
            for (Student x:students) {
                System.out.print(x.getName()+",");
            }
            if (hi.getHid()==1){
                List<Student> students1=mapper.getStudentByhhouseId(hi.getHid());
                    for (Student x:students1){
                        System.out.print(x.getName()+",");
                    }
            }
        System.out.print(")");
            System.out.println();
        }
//        Student a=new Student();
//       a.setName("小玲1");
//       a.setEmail("245@qq.com");
//        a.setDob(new Date());
//       mapper.insertStudent(a);
//        session.commit();
//       System.out.println("已添加"+a.getName());
    //通过Id修改学生信息
       s.setName("小花");
       s.setEmail("22qq.com");
       s.setDob(new Date());
       //此处s的Id来自S通过Id查询学tudent s =mapper.findStudentById(4);开始定义的，用于ID查询学生的Id
    mapper.updateStudent(s);
        session.commit();//如果Id是自增长，这句话有没有都会加1，所以会有很大的Id
     System.out.println("已修改"+s.getName());
     return null;
    }
}
